<template>
  <div class="container">
    <div v-loading="loading" class="patient-list">
      <div v-if="patients.length === 0 && !loading" class="no-data">
        <el-empty description="暂无患者信息" />
      </div>
      <div v-else v-for="patient in patients" :key="patient.id" class="patient-card" @click="goToPatientDetail(patient.id)">
      <div class="card-header">{{ patient.bedCode }}</div>
        <div class="card-body">
          <div class="patient-name">{{ patient.name }}</div>
          <div class="patient-info">
         
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue';
import { useRouter } from 'vue-router';
import { getCurrentInstance } from 'vue';
import { ElMessage } from 'element-plus';

const { proxy } = getCurrentInstance();
const router = useRouter();
const loading = ref(false);

onMounted(() => {
  getPatientList();
})

// 患者数据
const patients = ref([]);

// 跳转到患者详情页并传递患者编号和名称
const goToPatientDetail = (patientId) => {
  // 查找对应的患者
  const patient = patients.value.find(p => p.id == patientId);
  
  if (patient) {
    router.push({
      path: "/AddPatientReCord",
      query: {
        id: patient.id,
        patientCode: patient.patientCode,
        name: patient.name,
        bedCode: patient.bedCode,
        sex: patient.sex,
        age: patient.age,
        department: patient.department,
        doctor: patient.doctor,
        nurse: patient.nurse,
        diagnosis: patient.diagnosis,
        treatment: patient.treatment,
        nurseCare: patient.nurseCare,
        wardCode: patient.wardCode,
        officeCode: patient.officeCode,
      }
    });
  } else {
    ElMessage.error('未找到患者信息');
  }
}

const getPatientList = () => {
  loading.value = true;
  proxy.http.get('/api/PatientReCordController/GetPatientReCord').then(res => {
    if (res && Array.isArray(res)) {
      patients.value = res;
    } else {
      patients.value = [];
      ElMessage.warning('获取患者列表为空');
    }
  }).catch(err => {
    console.error('获取患者列表失败:', err);
    ElMessage.error('获取患者列表失败');
    patients.value = [];
  }).finally(() => {
    loading.value = false;
  });
}
</script>

<style scoped>
.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.patient-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 20px;
  margin-top: 30px;
  min-height: 200px;
}

.no-data {
  grid-column: 1 / -1;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 200px;
}

.patient-card {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  overflow: hidden;
  transition: all 0.3s ease;
  cursor: pointer;
}

.patient-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

.card-header {
  background-color: #409EFF;
  color: white;
  padding: 10px 15px;
  font-weight: bold;
  font-size: 16px;
}

.card-body {
  padding: 15px;
}

.patient-name {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 10px;
}

.patient-info {
  color: #606266;
  font-size: 14px;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.patient-info span {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
